﻿#include "daq_fileformatconverter.h"
#include "./ui_daq_fileformatconverter.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QDateTime>
#include <QDebug>
#include <QFile>


DAQ_FIleFormatConverter::DAQ_FIleFormatConverter(QWidget *parent)
  : QWidget(parent), ui(new Ui::DAQ_FIleFormatConverter) {
  ui->setupUi(this);

  QString iconPath = qApp->applicationDirPath() + "/Icon/lz.ico";
  this->setWindowIcon(QIcon(iconPath));
  this->setWindowTitle(u8"凌智DAQ文件格式转换工具");

  //默认勾选所有通道
  ui->checkBoxEnableChannel1->setChecked(1);
  ui->checkBoxEnableChannel2->setChecked(1);
  ui->checkBoxEnableChannel3->setChecked(1);
  ui->checkBoxEnableChannel4->setChecked(1);
  ui->checkBoxEnableChannel5->setChecked(1);
  ui->checkBoxEnableChannel6->setChecked(1);
  ui->checkBoxEnableChannel7->setChecked(1);
  ui->checkBoxEnableChannel8->setChecked(1);

  //设置采样率是否可编辑
  ui->lineEditSamplingRate->setEnabled(false);

  connect(ui->toolButtonSelectFile, &QToolButton::clicked, this, &DAQ_FIleFormatConverter::SelectFilePath);
  connect(ui->pushButton, &QPushButton::clicked, this, &DAQ_FIleFormatConverter::ActionConvert);
}

/************************************************************
** 函数名称: SelectFilePath
** 功能描述: 选择文件
** 输入参数: 
** 返 回 值: void
**************************************************************/
void DAQ_FIleFormatConverter::SelectFilePath() {

  read_file_path = QFileDialog::getOpenFileName(this, u8"选择读取文件",
                                                qApp->applicationDirPath(), u8"文件(*.bin *.csv)");

  // 取消时会返回空路径
  if (read_file_path.isEmpty()) {
    return;
  }

  if(file_info){
      delete file_info;
      file_info = nullptr;
  }

  file_info = new QFileInfo(read_file_path);
  if (!ParseFilePath(file_info->baseName())) {
    QMessageBox::critical(this, u8"提示", u8"文件选择错误");
    return;
  }
  if (file_info->suffix() != "bin" && file_info->suffix() != "csv") {
    QMessageBox::critical(this, u8"提示", u8"不支持该文件类型");
    return;
  }

  QString suffix = file_info->suffix();
  for (int i = 0; i < ui->comboBox_Transfer1->count(); i++) {
    if (ui->comboBox_Transfer1->itemText(i) == suffix) {
      ui->comboBox_Transfer1->setCurrentIndex(i);
      break;
    }
  }

  ui->lineEditSelectFile->setText(read_file_path);
}

/************************************************************
** 函数名称: ParseFilePath
** 功能描述: 解析文件名所包含的配置信息
** 输入参数:fileName只包含文件名，不包含后缀文件类型
** 返 回 值: void
**************************************************************/
bool DAQ_FIleFormatConverter::ParseFilePath(QString fileName) {
  //测试使用的文件名格式：DAQ122_20240327230228_10K.bin
  auto str_list = fileName.split("_");
  //if (str_list.size() != 4)return false;

  if(str_list.size() < 3){
      qDebug() << u8"文件格式错误！";
      return false;
  }

  device_name = str_list.at(0);
  auto sampleRateStr = str_list.at(2);
  if (sampleRateStr.endsWith("K")) {
    sampleRateStr = sampleRateStr.remove("K");
    sampleRateStr = sampleRateStr + "000";
  }

  sample_rate = sampleRateStr.toInt();

  ui->lineEditSamplingRate->setText(str_list.at(2));
  for (int i = 0; i < ui->comboBoxSelectDevice->count(); i++) {
    if (ui->comboBoxSelectDevice->itemText(i) == device_name) {
      ui->comboBoxSelectDevice->setCurrentIndex(i);
    }
  }
  return true;
}

/************************************************************
** 函数名称: Suffix2FileType
** 功能描述: 根据文件后缀名，返回文件类型
** 输入参数: suffix 文件后缀名
** 返 回 值: void
**************************************************************/
DAQ_FIleFormatConverter::FileType DAQ_FIleFormatConverter::Suffix2FileType(const QString &suffix) {
  if(suffix.isEmpty()){
    return FileType::FILETYPE_NONE;
  }

  QString s = suffix.toLower();

  if(s == "bin"){
    return FileType::FILETYPE_BIN;
  }else if(s == "csv"){
    return FileType::FILETYPE_CSV;
  }else{
    return FileType::FILETYPE_NONE;
  }

}

/************************************************************
** 函数名称: ActionConvert
** 功能描述: 选择保存文件路径后，开启转换文件线程
** 输入参数:
** 返 回 值: void
**************************************************************/
void DAQ_FIleFormatConverter::ActionConvert() {
  //获得checkbox是否勾选
  arr_enable_channel[0] = (ui->checkBoxEnableChannel1->checkState() == Qt::CheckState::Checked) ? (true) : (false);
  arr_enable_channel[1] = (ui->checkBoxEnableChannel2->checkState() == Qt::CheckState::Checked) ? (true) : (false);
  arr_enable_channel[2] = (ui->checkBoxEnableChannel3->checkState() == Qt::CheckState::Checked) ? (true) : (false);
  arr_enable_channel[3] = (ui->checkBoxEnableChannel4->checkState() == Qt::CheckState::Checked) ? (true) : (false);
  arr_enable_channel[4] = (ui->checkBoxEnableChannel5->checkState() == Qt::CheckState::Checked) ? (true) : (false);
  arr_enable_channel[5] = (ui->checkBoxEnableChannel6->checkState() == Qt::CheckState::Checked) ? (true) : (false);
  arr_enable_channel[6] = (ui->checkBoxEnableChannel7->checkState() == Qt::CheckState::Checked) ? (true) : (false);
  arr_enable_channel[7] = (ui->checkBoxEnableChannel8->checkState() == Qt::CheckState::Checked) ? (true) : (false);

  QString entirePath = qApp->applicationDirPath() + "/" + file_info->baseName();

  QString comboBox_Transfer1_str = ui->comboBox_Transfer1->currentText();
  QString comboBox_Transfer2_str = ui->comboBox_Transfer2->currentText();

  if (comboBox_Transfer1_str == comboBox_Transfer2_str) {
    QMessageBox::warning(this, u8"Failed!", u8"不能转换相同格式的文件");
    return;
  }

  // 获取输入文件格式和输出文件格式
  input_file_type = Suffix2FileType(comboBox_Transfer1_str);
  output_file_type = Suffix2FileType(comboBox_Transfer2_str);

  if (FILETYPE_CSV == input_file_type && FILETYPE_BIN == output_file_type) {
    save_file_path = QFileDialog::getSaveFileName(this, u8"保存文件", entirePath, "u8(*.bin)");

    if (save_file_path.isEmpty()) {
      QMessageBox::warning(this, u8"Failed!", u8"文件路径不能为空！");
      return;
    }

    c_thread = new CSV2BINThread(read_file_path, save_file_path, sample_rate, arr_enable_channel);
  } else if (FILETYPE_BIN == input_file_type && FILETYPE_CSV == output_file_type) {
    save_file_path = QFileDialog::getSaveFileName(this, u8"保存文件", entirePath, "u8(*.csv)");

    if (save_file_path.isEmpty()) {
      QMessageBox::warning(this, u8"Failed!", u8"文件路径不能为空！");
      return;
    }

    c_thread = new BIN2CSVThread(read_file_path, save_file_path, sample_rate, arr_enable_channel);
  }else{
    QMessageBox::warning(this, u8"Failed!", u8"不支持该文件类型转换");
  }

  connect(this->c_thread, &ConvertThread::QuitThread, this, &DAQ_FIleFormatConverter::FinshiedThread);

  c_thread->start();
}

void DAQ_FIleFormatConverter::FinshiedThread() {
  QMessageBox::information(this, u8"通知", u8"文件成功生成");
}


DAQ_FIleFormatConverter::~DAQ_FIleFormatConverter() {
  delete ui;
}
